package com.fable.common.aop;

import com.alibaba.fastjson.JSON;
import com.fable.common.annotation.SaveSysLog;
import com.fable.common.utils.IPUtils;
import com.fable.common.utils.ShiroUtils;
import com.fable.sgms.syslog.dao.entity.SysLog;
import com.fable.sgms.syslog.dao.entity.SysLogDetail;
import com.fable.sgms.syslog.service.SysLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class SysLogAspect {

    @Resource
    private SysLogService sysLogService;

    @Pointcut("@annotation(com.fable.common.annotation.SaveSysLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object saveSysLog(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        SysLog sysLog = new SysLog();
        SaveSysLog syslog = method.getAnnotation(SaveSysLog.class);
        if (syslog != null) {
            //注解上的描述
            sysLog.setOperation(syslog.value());
        }

        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");

        //请求的参数
        Object[] args = joinPoint.getArgs();


        String params = JSON.toJSONString(args);
        sysLog.setParams(params);

        //获取request
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));

        //设置请求地址
        String url = request.getServletPath();
        sysLog.setUrl(url);

        String username = (String) ShiroUtils.getSubject().getPrincipal();
        sysLog.setUsername(username);

        sysLog.setCreateTime(new Date());

        sysLog.setResultStatus(1);

        //保存系统日志
        sysLogService.save(sysLog);

        Object obj;

        try {
            obj = joinPoint.proceed(joinPoint.getArgs());
        } catch (Throwable throwable) {

            SysLog updateLog = SysLog.builder().ip(sysLog.getId()).resultStatus(0).build();
            sysLogService.update(updateLog);

            //插入详细信息
            SysLogDetail detail = SysLogDetail.builder().logId(sysLog.getId()).detail(throwable.getMessage()).build();
            detail.setCreateTime(new Date());
            sysLogService.saveLogDetail(detail);

            throw throwable;
        }

        return obj;
    }

}
